package com.dolphin.common.component.var.model;

import com.dolphin.common.component.var.enums.ValueTypeEnum;
import com.dolphin.common.utils.JsonInterface;
import com.dolphin.common.utils.TypeUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

/**
 * @author shida
 */
@Getter
@Setter
@ToString
public class GlobalVarDefinition {
    private static final String SCRIPT_CONTENT = "scriptContent";

    private String type;
    private String subType;
    private String params;
    private List<String> inputs;

    public Object getParamValue(String name) {
        if (params == null) {
            return null;
        }
        List<VarParam> varParams = JsonInterface.jsonArr2ListByStr(params, VarParam.class);
        for (VarParam varParam : varParams){
            if(varParam.getName().equals(name)){
                ValueTypeEnum byType = ValueTypeEnum.getByType(varParam.getType());
                if(byType == null){
                    return varParam.getValue();
                }
                return TypeUtils.cast(varParam.getValue(), byType.getClazz());
            }
        }

        return null;
    }
}
